import { Injectable } from '@angular/core';
import {RequestUtil} from "../../../util/request-util";
import {Observable} from "rxjs";
import {MsgResult} from "../../../models/msg-result";
import {API} from "../../../constants/constants";

@Injectable()
export class MenusService {

  constructor(
    private requestUtil : RequestUtil
  ) { }

  /**
   * 初始化菜单
   * @returns {Observable<MsgResult>|any}
   */
  getMenus():Observable<MsgResult>{
      return this.requestUtil.get(API.getMenus);
  }

  /**
   *  添加菜单
   * @param menu
   * @returns {Observable<MsgResult>|any}
   */
  insertMenu(menu):Observable<MsgResult>{
    let params="name="+menu.name+"&url="+menu.url+"&orderBy="+menu.orderBy+"&isDisplay="+menu.isDisplay+
      "&remark="+menu.remark+"&isEnable="+menu.isEnable+"&menuCode="+menu.menuCode+
      "&pId="+menu.pId+"&cssName="+menu.cssName+"&type="+menu.type+"&isBlank="+menu.isBlank+
      "&isExistUrl="+menu.isExistUrl+"&isExternal="+menu.isExternal;
    return this.requestUtil.post(API.insertMenu,params);
  }

  /**
   * 删除菜单
   * @param id :菜单id
   * @returns {Observable<MsgResult>|any}
   */
  deleteMenu(id):Observable<MsgResult>{
    let params="id="+id;
    return this.requestUtil.get(API.deleteMenu,params);
  }

  /**
   *  批量删除接口
   * @param menuIdArr: id数组
   * @returns {Observable<MsgResult>|any}
   */
  delMenus(menuIdArr):Observable<MsgResult>{
    let params="menuIdArr="+menuIdArr;
    return this.requestUtil.post(API.delMenus,params);
  }

  /**
   *  批量删除接口
   * @param menuIdArr: id数组
   * @returns {Observable<MsgResult>|any}
   */
  forceDelMenus(menuIdArr):Observable<MsgResult>{
    let params="menuIdArr="+menuIdArr;
    return this.requestUtil.post(API.forceDelMenus,params);
  }

  /**
   *
   * @param id :菜单id
   * @returns {Observable<MsgResult>|any}
   */
  getMenuById(id): Observable<MsgResult> {
    let params = "id=" + id;
    return this.requestUtil.get(API.getMenuAndDealMenuCodeById, params);
  }

  /**
   * 菜单信息
   * @param menu : 菜单对象
   * @returns {Observable<MsgResult>|any}
   */
  updateMenu(menu): Observable<MsgResult> {
    let params = "id=" + menu.id + "&pId=" + menu.pId + "&name=" + menu.name + "&url=" + menu.url + "&orderBy=" + menu.orderBy +
      "&isDisplay=" + menu.isDisplay + "&remark=" + menu.remark + "&isEnable=" + menu.isEnable + "&menuCode=" + menu.menuCode
      + "&cssName=" + menu.cssName + "&type=" + menu.type + "&isBlank=" + menu.isBlank + "&isExistUrl=" + menu.isExistUrl + "&isExternal=" + menu.isExternal;
    return this.requestUtil.post(API.updateMenu, params);
  }

  /**
   *
   * @param id:菜单id
   * @param newMenuId：新菜单id
   * @returns {Observable<MsgResult> | any}
   */
  saveOperButtonByMenuId(menuId,newMenuId){
    let params="menuId="+menuId+'&pId='+newMenuId;
    return this.requestUtil.post(API.saveOperButtonByMenuId,params);
  }

  deleteOperationById(id):Observable<MsgResult>{
    let params="id="+id;
    return this.requestUtil.get(API.deleteOperation,params);
  }

  delOperations(operIdArr):Observable<MsgResult>{
    let params="operationIdArr="+operIdArr;
    return this.requestUtil.post(API.delOperations,params);
  }

  addOrUpdateOpreations(params,menuId):Observable<MsgResult>{
    return this.requestUtil.post(API.saveOrUpdateOpreations.replace("${menuId}",menuId),params);
  }
  /**
   * 获取菜单
   */
  getMenusTree(): Observable<MsgResult> {
    return this.requestUtil.get(API.getPermissions+"menus");
  }

}
